package com.wf.ew.system.biz.impl;

import com.wf.ew.common.BaseDao;
import com.wf.ew.common.constant.UserConstant;
import com.wf.ew.common.utils.Utils;
import com.wf.ew.system.biz.RoleBiz;
import com.wf.ew.system.model.Role;
import com.wf.ew.system.model.User;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2019-09-28 23:36:08
 * @version ：1.0
 */
@Repository
public class RoleBizimpl extends BaseDao implements RoleBiz {

	@Override
	public List<Role> list(Role role, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Role> rm = new BeanPropertyRowMapper<Role>(Role.class);
		
		StringBuffer sql = new StringBuffer("select r.role_id, r.role_name, r.comments,"
			+" r.create_time, r.update_time, ");
		sql.append(" r.companyId, cmy.name companyName ");
		sql.append(" from sys_role r ");
		sql.append(" inner join we_company cmy on cmy.id=r.companyId ");
        sql.append(" where r.is_delete=0 ");
        if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and r.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
		if(Utils.isNotEmpty(role)){
			if(Utils.isNotEmpty(role.getRoleName())){
				sql.append(" and instr(r.roleName,:roleName)>0 ");
				map.put("roleName", role.getRoleName());
			}
//			if(Utils.isNotEmpty(role.getSort())&&Utils.isNotEmpty(user.getOrder())){
//				sql.append(" order by "+user.getSort()+" "+user.getOrder());
//			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public boolean add(Role role, User loginUser) {
		int count = 1;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into sys_role (");
		sql.append("companyId,role_name,comments");
		sql.append(") values ( ");
		sql.append(":companyId,:role_name,:comments");
		sql.append(")");
		map.put("role_id", role.getRoleId()+"");
		map.put("companyId", loginUser.getCompanyId());
		map.put("role_name", role.getRoleName());
		map.put("comments", role.getComments());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}
}
